package com.mwy.springbootrabbitmq.mq.direct.send;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 路由队列--生产者
 */
public class Send {

    private final static String EXCHANGE_NAME = "exchange_direct" ;

    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //连接工厂的基本配置
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/mwy");

        try(
                Connection connection = factory.newConnection();
                Channel channel = connection.createChannel();
        ){
            //声明交换机
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            String message = "路由队列消息";
            /**
             * 发送消息
             *  交换机名
             *  routingKey路由键
             *  基本属性
             *  消息内容
             */
            channel.basicPublish(EXCHANGE_NAME,"green",null,message.getBytes(StandardCharsets.UTF_8));
            System.out.println("生产者生产消息："+message);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
